package com.tlcx.kfip.fragment.main;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.tlcx.kfip.R;
import com.tlcx.kfip.activity.message.MessageCenterAct;
import com.tlcx.kfip.adapter.KfipListAdapter;
import com.tlcx.kfip.entity.KfipListEntity;
import com.tlcx.kfip.fragment.base.BaseFragment;
import com.tlcx.kfip.listener.DividerGridItemDecoration;
import com.tlcx.kfip.listener.OnClickMenuListener;
import com.tlcx.kfip.ui.KfipAdsView;
import com.tlcx.kfip.ui.KfipFilterView;
import com.tlcx.kfip.ui.KfipMenuView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 功夫夺宝碎片
 * Created by victor on 2016/10/1 20:44.
 * Email:zhuzhaoyang@yuanchuangyun.com
 */
public class KFIPFragment extends BaseFragment{

    @BindView(R.id.xrv_kfip)
    XRecyclerView xRecyclerView;

    private int mPage = 1;
    private Unbinder unbinder;
    private Handler mHanlder;
    private KfipListAdapter mAdapter;
    private List<KfipListEntity> mData;              //数据源


    @Override
    protected int getContentView() {
        return R.layout.frag_kfip;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);

        mData = new ArrayList<>();
        mHanlder = new Handler();
        mAdapter = new KfipListAdapter(getActivity());

        //初始化网格列表的管理器
        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        manager.setOrientation(LinearLayoutManager.VERTICAL);

        //设置xRecyclerView的属性
        xRecyclerView.setLayoutManager(manager);
        xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        xRecyclerView.setArrowImageView(R.mipmap.framework_arrow_refresh_down);
        xRecyclerView.addItemDecoration(new DividerGridItemDecoration(getActivity()));

        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mHanlder.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadFirstPageData();
                    }
                },1000);
            }

            @Override
            public void onLoadMore() {
                mPage++;
                loadNextPageData(mPage);
            }
        });

        //添加头部1
        KfipAdsView kfipAdsView = new KfipAdsView(getActivity());
        xRecyclerView.addHeaderView(kfipAdsView);

        //添加头部2
        KfipMenuView kfipMenuView = new KfipMenuView(getActivity());
        kfipMenuView.setClickMenuListener(new OnClickMenuListener() {
            @Override
            public void onClick(int index) {
                switch (index){
                    default:
                        showShortToast(""+index);
                        break;
                }
            }
        });
        xRecyclerView.addHeaderView(kfipMenuView);

        //添加头部3
        KfipFilterView filterView = new KfipFilterView(getActivity());
        xRecyclerView.addHeaderView(filterView);

        xRecyclerView.setAdapter(mAdapter);

        xRecyclerView.setRefreshing(true);
    }

    /**
     * 刷新加载第一页数据
     */
    private void loadFirstPageData(){
        mPage = 1;
        mData.clear();

        KfipListEntity entity1 = new KfipListEntity("2017最新款奔驰GLK500纯金色打造",R.mipmap.mock_car_1,20);
        KfipListEntity entity2 = new KfipListEntity("双十一京东购物大狂欢特惠50元充值卡",R.mipmap.mock_jingdong_1,80);
        KfipListEntity entity3 = new KfipListEntity("京东E卡经典卡5000面值",R.mipmap.mock_jingdong_2,10);
        KfipListEntity entity4 = new KfipListEntity("中国移动200充值卡",R.mipmap.mock_phone_fee,40);
        KfipListEntity entity5 = new KfipListEntity("劲爽超值充30元得100QQ币",R.mipmap.mock_qq_coin,90);
        KfipListEntity entity6 = new KfipListEntity("宝马2017款320Li时尚型",R.mipmap.mock_car_2,50);
        mData.add(entity1);
        mData.add(entity2);
        mData.add(entity3);
        mData.add(entity4);
        mData.add(entity5);
        mData.add(entity6);


        mAdapter.addAllAndNotify(mData);
        xRecyclerView.refreshComplete();
    }

    /**
     * 获得第page页数据
     * @param page
     */
    private void loadNextPageData(int page){
        List<KfipListEntity> data =new ArrayList<>();

        KfipListEntity entity1 = new KfipListEntity("2017最新款奔驰GLK500纯金色打造",R.mipmap.mock_car_1,20);
        KfipListEntity entity2 = new KfipListEntity("双十一京东购物大狂欢特惠50元充值卡",R.mipmap.mock_jingdong_1,80);
        KfipListEntity entity3 = new KfipListEntity("京东E卡经典卡5000面值",R.mipmap.mock_jingdong_2,10);
        KfipListEntity entity4 = new KfipListEntity("中国移动200充值卡",R.mipmap.mock_phone_fee,40);
        KfipListEntity entity5 = new KfipListEntity("劲爽超值充30元得100QQ币",R.mipmap.mock_qq_coin,90);
        KfipListEntity entity6 = new KfipListEntity("宝马2017款320Li时尚型",R.mipmap.mock_car_2,50);

        data.add(entity1);
        data.add(entity2);
        data.add(entity3);
        data.add(entity4);
        data.add(entity5);
        data.add(entity6);

        mAdapter.addAllAndNotify(data);
        xRecyclerView.loadMoreComplete();
    }
    /**
     * 跳转搜索页
     */
    @OnClick(R.id.iv_frag_kfip_search)
    public void skipToSearch(){

    }

    /**
     * 跳转消息页
     */
    @OnClick(R.id.iv_frag_kfip_message)
    public void skipToMessage(){
        startActivity(MessageCenterAct.newIntent(getActivity()));
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
